export default {
	data() {
		return {
			tabs: [{
				title: '待接单',
				badge: 0,
				id: 0,
				isdisplay: true,
			    isbadgedisplay: true,
			}, {
				title: '待处理',
				badge: 0,
				id: 1,
				isdisplay: true,
				isbadgedisplay: true,
			}, {
				title: '提交完工',
				badge: 0,
				id: 2,
				isdisplay: true,
				isbadgedisplay: true,
			}, {
				title: '已完工',
				badge: 0,
				id: 3,
				isdisplay: true,
				isbadgedisplay: true,
			}],
		}
	},
	methods: {


		//预约
		async OrderReservationAsync(AppointmentTime, Remark, ReasonCodeId) {
			let result = await this.workorderdetailservice.WOAppointmentAsync(this.UserInfo.mobile, this.orderDetailData.WoId,
				AppointmentTime, Remark,
				ReasonCodeId, this.UserInfo.RetId);
			console.log(result);
			if (result.RetBool == true) {
				uni.showToast({
					title: "已成功预约",
					icon: "none"
				});
				uni.hideLoading();
				this.$refs.reservationPoPup.closePoPup();
				this.RefreshWorkOrder();
			} else {
				uni.hideLoading();
				uni.showToast({
					title: result.RetMsg,
					icon: "none"
				});
			}
		},

		//根据经纬度查出地址名称
		async getLocationAddress(lon, lat) {
			let params = {
				location: `${lat},${lon}`
			}
			let result = await this.mapApiService
				.geoMultipParamMapAsync(this.qqMapServiceUrl, "/ws/geocoder/v1", params);
			return result;
		},

		//地址转经纬度
		async getAddressMapAsync(address) {
			let result = await this.mapApiService.geoCommMapAsync(this.qqMapServiceUrl,
				"/ws/geocoder/v1",
				"address",
				address);
			return result;
		},

		//根据经纬度计算距离
		async calcMapDistance(currentLongitude, currentLatitude, accountLongitude, accountLatitude) {
			let form = new Array(currentLatitude, currentLongitude);
			let to = new Array(accountLatitude, accountLongitude);
			let params = {
				mode: "driving",
				from: form.toString(),
				to: to.toString(),
			};
			let {
				status,
				result
			} = await this.mapApiService.geoMultipParamMapAsync(this.qqMapServiceUrl, "/ws/distance/v1", params);
			console.log(result);
			if (status == 0) {
				//距离-米
				let distance = result.elements[0].distance;

				return distance / 1000;

			}
			return 0;
		},

		//根据地址名称获取经纬度
		async getLonglatByGPS(address, token) {
			let {
				status,
				result
			} = await this.getAddressMapAsync(address);
			if (status == 0) {
				//console.log(result);
				this.accountLongitude = result.location.lng;
				this.accountLatitude = result.location.lat;

				this.VisitData.accountLongitude = result.location.lng;
				this.VisitData.accountLatitude = result.location.lat;
				this.VisitData.IsLocationSuccess = true;
			}
		},

		async setLocationDistance(res) {
			this.currentLongitude = res.longitude;
			this.currentLatitude = res.latitude;
			this.VisitData.currentLongitude = res.longitude;
			this.VisitData.currentLatitude = res.latitude;
			let result = await this.getLocationAddress(this.currentLongitude, this.currentLatitude);
			this.VisitData.Location = result.result.address;
			// console.log(`me.userPosition${me.userPosition}`)
			// console.log(`me.orderDetailData ${me.orderDetailData}`)
			this.getLonglatByGPS(this.orderDetailData.DisplayAdress,
				this.UserInfo.RetId);
			//this.VisitData.Milestone = distance
			//console.log(this.VisitData.Milestone);

			uni.hideLoading()
		},

		//上门组件打开时
		VisitPoPupOpen() {
			uni.showLoading({
				title: "打开...",
				mask: true
			})
			this.$refs.visitPoPup.openPoPup();

		},



		async GetUserLocation(callback) {
			//debugger
			let me = this;
			this.VisitData.IsLocationSuccess = false;
			// uni.getLocation({
			// 	type: 'gcj02',
			// 	success: (res) => {
			// 		this.setLocationDistance(res)
			// 	},
			// 	fail: (res) => {
			// 		if (res.errMsg == "getLocation:fail auth deny") {
			// 			uni.showModal({
			// 				title: '是否授权当前位置',
			// 				content: '需要获取您的地理位置，请确认授权',
			// 				success: (tip) => {
			// 					if (tip.confirm) {
			// 						uni.openSetting({
			// 							success: (data)=> {
			// 								if (data.authSetting["scope.userLocation"] === true) {
			// 									uni.getLocation({
			// 										type: 'gcj02',
			// 										success: (res)=> {
			// 											this.setLocationDistance(res)
			// 										}
			// 									});
			// 								}
			// 							},
			// 							fail: (error)=> {
			// 								wx.showToast({
			// 									title: '调用授权窗口失败',
			// 									icon: 'none',
			// 									duration: 1000
			// 								})
			// 							}
			// 						})
			// 					}
			// 				}
			// 			})
			// 		}
			// 	}
			// });


			try {
				let mylocation = await this.uniApiPromise.GetLocationPromise();

				this.setLocationDistance(mylocation);
			} catch (error) {
				if (error.errMsg == "getLocation:fail auth deny") {
					let modelRes = await this.uniApiPromise.showModalPromise();
					if (modelRes && modelRes.confirm) {
						uni.hideLoading();
						let settingRes = await this.uniApiPromise.OpenSettingPromise();
						if (settingRes &&
							settingRes.authSetting["scope.userLocation"] === true) {
							let mylocation = await this.uniApiPromise.GetLocationPromise();
							this.setLocationDistance(mylocation);
						} else {
							uni.hideLoading();
						}
					} else {
						uni.hideLoading();
						uni.showToast({
							icon: "none",
							title: "没有授权"
						})
					}
				}
			}
		},

		//工单-上门
		async WoFieldServiceAsync(WOFieldService, token) {
			let result = await this.workorderdetailservice.woFieldServiceAsync(WOFieldService, token);
			console.log(result);
			if (result.RetBool == true) {
				uni.showToast({
					title: "已确定上门",
					icon: "none"
				});
				uni.hideLoading();
				this.$refs.visitPoPup.closePoPup();
				this.RefreshWorkOrder();
			} else {
				uni.hideLoading();
				uni.showToast({
					title: result.RetMsg,
					icon: "none"
				});
			}
		},
		//工单接单
		async AcceptorderAsync(WoId) {
			console.log(WoId);
			let uPhone = this.UserInfo.mobile;
			let token = this.UserInfo.RetId;
			let result = await this.workorderlistervice.WOTakingAsync(uPhone, WoId, token);
			return result;
		},

		//刷新工单列表
		RefreshWorkOrder() {
			this.workOrderList = [];
			this.pageNum = 1;
			this.getPageWorderOrderStateAsync();
		},

		async getPageWorderOrderStateAsync(callfunc) {
			let userPhone = this.UserInfo.mobile;
			let token = this.UserInfo.RetId;
			let pageSize = this.PageSize;
			let pageNum = this.pageNum;

			let result = await this.workorderlistervice.woListAsync(userPhone, pageNum,
				pageSize, this.workOrderState, token);
			if (result.RetBool && result.RetObject) {
				let workOrderPage = JSON.parse(result.RetObject);
				this.workOrderList.push(workOrderPage);

				if (result.RetId) {
					let workOrderCount = JSON.parse(result.RetId);

					for (let tabindex in this.tabs) {
						if (tabindex == 0)
							this.tabs[tabindex].badge = workOrderCount.WaitingOrder
						if (tabindex == 1)
							this.tabs[tabindex].badge = workOrderCount.WaitingHandle
						if (tabindex == 2)
							this.tabs[tabindex].badge = workOrderCount.SubmitFinished
						if (tabindex == 3){
							this.tabs[tabindex].badge = workOrderCount.Finished
							this.tabs[tabindex].isbadgedisplay = false
						}
							
					}
					console.log(workOrderCount);
				}
			}else{
				uni.showToast({
					icon:"none",
					title: result.RetMsg
				})
			}
			if (callfunc && typeof callfunc == "function") {
				callfunc();
			}
			uni.hideLoading();
		},

		async WeChatExpAsync() {
			let userPhone = this.UserInfo.mobile;
			let token = this.UserInfo.RetId;
			let result = await this.workorderlistervice.WeChatExpAsync(userPhone, token);
			return result;
		},

	}
}
